function data = data_coefficient(coef, options, varargin)
%DATA = DATA_COEFFICIENT( COEF, OPTIONS )
%  According to settings in OPTIONS, extract various data from coefficient
%  array COEF. OPTIONS is a struct, to enable an option, you need to call 
%  OPTIONS.xxx = 1;
%  where xxx is listed below (case sensitive, quoto not included):
%    "origin" : original data (COEF) set to data.origin
%    "block" : divide data (COEF) into seperate block, saved in data.block
%    "strip" : divide data (COEF) into seperate strip, saved in data.strip
%    "up", "down", "left", "right" :
%    "block_up", "block_down", "block_left", "block_right" :
%    "div2"
    if nargin < 2
        options.origin = 1;
    end
    
    data = [];
    if isfield(options,'origin') && (options.origin == 1)
        data.origin = coef;
    end
    if isfield(options,'block') && (options.block == 1)
        data.block = blocklize(coef);
    end
    if isfield(options,'strip') && (options.strip == 1)
        data.strip = striplize(coef);
    end
end

function b = blocklize(coef, sz, fill_type)
    if (nargin < 2)
        % default value for JPEG block size
        sz = [8 8];
    end
    if (nargin < 3) 
        fill_type = 0;
        % unused now, 
    end
    M = sz(1);
    N = sz(2);
    [P,Q] = size(coef);
    if (mod(P, M) ~= 0) || (mod (Q, N) ~= 0)
        error('size of coef cannot divide sz.');
    end
    count = 1;
    b = {};
    for i = 1:P/M
        for j = 1:Q/N
          b{count} = coef(i*M-M+1:i*M,j*N-N+1:j*N);
          count = count + 1;
        end
    end
end

function s = striplize(coef, sz)
    if (nargin < 2)
        % default value for JPEG block size
        sz = [8 8];
    end
    M = sz(1);
    N = sz(2);
    [P,Q] = size(coef);
    count = 1;
    s = {};
    for i = 1:M
        for j = 1:N
            s{count} = coef(i:M:P, j:N:Q);
            count = count + 1;
        end
    end
end
